#include <assert.h>
#include <imbus.h>
#include <stdio.h>
#include <unistd.h>
#include "im_internals.h"

#define NO_TEST_OBJECT_MAIN
#include "test_object.h"
#include "test_object.c"

static const IMChar *test_server_address = "local:/tmp/im-test-server";

int main (int argc, char *argv [])
{
    IMConnection *conn;
    IMIOChannel  *in;
    IMStream     *stream;
    IMInt         timeout = -1;
    IMIOError     error = IM_IO_ERROR_NONE;
    IMPointer     obj;
    IMType        type;

    im_init(&argc, &argv);

    (void) TYPE_POINT;
    (void) TYPE_CIRCLE;

#if IM_ENABLE_DEBUG
    _im_slice_print_debug_info();
#endif

    conn = im_connection_new_connect (test_server_address, &error);
    if (!conn) {
        fprintf (stderr, "Failed to connect to server %s, error %d.\n",
                 test_server_address, error);
        return 1;
    }

    stream = im_text_stream_new ();
    in  = im_io_channel_unix_new (0);

    if (!im_stream_read (stream, in, &timeout, &error)) {
        fprintf (stderr, "Failed to read text stream.\n");
        return 1;
    }
    while (1) {
        type = im_stream_get_data_type (stream);
        if (type == IM_TYPE_INVALID)
            break;
        if (IM_TYPE_IS_OBJECT(type)) {
            obj = im_object_new (type);
            if (im_stream_get_object (stream, obj)) {
                if (!im_connection_send (conn, obj, &timeout, &error)) {
                    fprintf (stderr, "Failed to send object, error %d.\n", error);
                }
            }
            im_object_unref (obj);
            sleep (1);
        } else {
            im_stream_skip_data (stream);
        }
    }

    im_object_unref(conn);
    im_object_unref(in);
    im_object_unref(stream);

#if IM_ENABLE_DEBUG
    _im_slice_print_debug_info();
#endif

    return 0;
}
/*
vi:ts=4:nowrap:ai:expandtab
*/
